'''
Created on Jun 2, 2011

@author: Jialai_Zhu
'''
'''
Created on Jun 2, 2011

@author: Jialai_Zhu
'''
import unittest
import PyV8
class document(PyV8.JSClass):
    class cookie(PyV8.JSClass):
        def __init(self):pass
    cookie=''
class Location(PyV8.JSClass):
    def __init__(self,Global,url):
        self.Global=Global
        self.url=url
    def reload(self):
        print 'location.reload'
        print self.Global.document.cookie
    def assign(self,url):
        print url
class windows(PyV8.JSClass):      # define a compatible javascript class
    def __init__(self,url):               # define a method
        self.location=Location(self,url)
        self.document=document()
    def report(self):
        try:
            print self.document.cookie
        except:
            pass
        
def js_302(url,code):        
    global_oject=windows(url)
    ctxt = PyV8.JSContext(global_oject)          # create a context with an implicit global object
    ctxt.enter()                     # enter the context (also support with statement)
    
    ctxt.eval(code)                 # evalute the javascript expression
    vars=ctxt.locals
    class result:
        cookie=None
        location=None
        try:
            cookie=vars.document.cookie
        except:pass
        try:
            if isinstance(vars.location,Location):
                location=vars.location
                location=location.url
            else:
                location=vars.location
        except Exception,ex:
            pass
    return result()
if __name__ == '__main__':
    url=''
    code3='''var mycookie="87c85f0f4a1433cf2fb421769f26407690aab46053d911e1";
    var td=new Date();
    td.setDate(td.getDate()+30);
    document.cookie="yetet3uu=checkcode="+mycookie+";expires="+td.toGMTString()+";path=/;";
    var myurl="/index.asp";
    location=myurl;
    '''
    code2="var td=new Date();td.setDate(td.getDate()+30);document.cookie='checkcode=bnmp912873;expires='+td.toGMTString()+';path=/;';location.reload();"
    code4='''var myurl="/?";var u1="f39ec3";var u2="e07bf39ec3da";var u3="c3dad1";var u4="d92c1a";myurl+=u2.substr(0,6);myurl+="=";myurl+=u4;location=myurl;'''        
    ret=js_302(url,code3)
    ret=js_302(url,code2)
    ret=js_302(url,code4)
    pass